/**
*	Group: Y1
*	The LobbyPanel class renders the lobby screen which is designed for connecting the users that are going to play the game
*	@author Jessica Newman
*/
package lightspeed.Media.Graphics;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.imageio.ImageIO;
import java.io.*;
import java.awt.image.BufferedImage;
import java.net.*;
import lightspeed.Network.*;

public class LobbyPanel extends JPanel
{
	/**
	*Initializes WindowManager
	**/
	WindowManager wm = null;
	/**
	*Initialize the BufferedImage which will direct the user where to enter an IP
	**/
	BufferedImage ipic = null;
	BufferedImage userpic = null;
	/**
	*Initialize the BufferedImage which will separate the chat room from the rest of the JComponents
	**/
	BufferedImage separator = null;
	
	boolean hosting = false;
	
	/**
	*Initializes all JComponents
	**/
	public static double maxWidth=(double)WindowManager.WINDOW_WIDTH;
	public static double maxHeight=(double)WindowManager.WINDOW_HEIGHT;
	public static int ipField_left=(int)(maxWidth/2.0);
	public static int ipField_top=(int)(maxHeight/2.0);
	public static int hostField_top=(int)(maxHeight/3.0);
	public static int hostField_left=ipField_left-150;
	public static int hostButton_left=ipField_left-100;
	public static int hostButton_top=(int)(maxHeight/5.0);
	public static int nameField_left=ipField_left;
	public static int nameField_top=(int)(maxHeight/10.0);
	public static JTextField ipField = new JTextField("", 15);
	public static JTextField hostField = new JTextField("You are host! Your IP is: ");
	public static JTextField nameField = new JTextField("Newbie");
	public static JButton ipButton = new JButton();
	public static JButton hostButton = new JButton();
	public static Image okImg = null;
	public static Image ok2Img = null;
	public static Image host1Img = null;
	public static Image host2Img = null;
	public static ImageIcon host1Icon = null;
	public static ImageIcon host2Icon = null;
	public static ImageIcon okIcon = null;
	public static ImageIcon ok2Icon = null;
	
	/**
	*	Constructor: creates an instance of LobbyPanel
	*	When this constructor is invoked, the Lobby screen is rendered as all the JComponents
	*	are read in and drawn to the menu screen.
	*/
	public LobbyPanel(WindowManager window)
	{
		wm=window;
		setLayout(null);
		setBackground(Color.black);
		try
		{
			ipic = ImageIO.read(new File("images/ipic.png"));
			userpic = ImageIO.read(new File("images/userbutton.png"));
			separator = ImageIO.read(new File("images/separator.png"));
		}
		catch(IOException e)
		{
			System.out.println(e);
		}
		wm.requestFocus();
		okImg = getToolkit().createImage("images/ok.png");
		ok2Img = getToolkit().createImage("images/ok2.png");
		host1Img = getToolkit().createImage("images/host2.png");
		host2Img = getToolkit().createImage("images/host1.png");
		okIcon = new ImageIcon(okImg);
		ok2Icon = new ImageIcon(ok2Img);
		host1Icon = new ImageIcon(host1Img);
		host2Icon = new ImageIcon(host2Img);
		ipButton.setIcon(okIcon);
		ipButton.setPressedIcon(ok2Icon);
		ipButton.setBorderPainted(false);
		ipButton.addActionListener(this.new buttonListener());
		hostButton.setIcon(host1Icon);
		hostButton.setPressedIcon(host2Icon);
		hostButton.setBorderPainted(false);
		hostButton.setContentAreaFilled(false);
		hostButton.addActionListener(this.new buttonListener());

		add(nameField);
		nameField.setEditable(true);
		nameField.setBounds(nameField_left,nameField_top,150,20);
		add(hostButton);
		hostButton.setBounds(hostButton_left,hostButton_top,200,82);
		add(ipField);
		ipField.setEditable(true);
		ipField.setBounds(ipField_left,ipField_top,150,20);
		add(ipButton);
		ipButton.setBounds(ipField_left+152,ipField_top,42,20);
		wm.requestFocus();
		
		setVisible(true);
    }
	
	/**
	*	Creates the images at the specified coordinates.
	*	drawImage function takes the image, x coordinate, y coordinate, and an ImageObserver as parameters
	*	@param g Graphics object in order to draw the images onto the menu screen
	*/
	public void paintComponent(Graphics g)
	{
		super.paintComponent(g);
		g.drawImage(ipic,ipField_left-253,ipField_top-12,null);
		g.drawImage(userpic,ipField_left-253,nameField_top-16,null);
		g.drawImage(separator,(int)(maxWidth/2-350),(int)(maxHeight/2.0)+100,null);
	}
	
	/**
	*	Clears all graphical components so they won't steal focus.
	*/
	public void removeComponents()
	{
		remove(hostButton);
		remove(ipField);
		remove(ipButton);
		remove(nameField);
	}
	
	/**
	*	Adds/Restores graphical components so they can be used in the lobby panel.
	*/
	public void addComponents()
	{
		add(hostButton);
		add(ipField);
		add(ipButton);
		add(nameField);
	}
	
	/**
	* Tells the system what to do when a button is pressed. When the host button is pressed, a host field appears
	* displaying the host's IP address, and WindowManager starts the server. When the "OK" button is pressed, an instance
	* of the LightClient is created.
	* @param e ActionEvent object in order to let the system know when a button is pressed
	**/
	private class buttonListener implements ActionListener
	{
		public void actionPerformed(ActionEvent e)
		{
			if(e.getSource()==hostButton)
			{
				System.out.println("Host button pressed!");
				add(hostField);
				hostField.setEditable(false);
				hostField.setBounds(hostField_left,hostField_top+10,300,60);
				hostField.setFocusable(false);
				
				//create host in window manager
				if(!hosting)
				{
					wm.startServer();
					try
					{
						hostField.setText("You are host! Your IP is: " + java.net.InetAddress.getLocalHost().getHostAddress());
					}
					catch(IOException ioe)
					{
						hostField.setText("Could not display host IP");
					}
					hosting = true;
				}
				//
			}
			else if(e.getSource()==ipButton)
			{
				System.out.println("IP Button pressed!");
				try
				{
					WindowManager.client = new LightClient(ipField.getText(), nameField.getText());
					wm.setTopLayer(wm.GAME_PANEL);
				}
				catch(IOException ioe)
				{
					ipField.setText("Invalid IP");
					WindowManager.client = null;
				}
			}
			wm.requestFocus();
		}
	}
	
}